﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibHuman;
namespace Human
{
	/// <summary>
	/// Summary description for frmMucLuongBQ.
	/// </summary>
	public class frmMucLuongBQ : System.Windows.Forms.Form
	{
		private AccessData  mDB=new AccessData ();
        Language lan = new Language();
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.RadioButton rdtToanCongTy;
		private System.Windows.Forms.Button butChon;
		private System.Windows.Forms.Button butKeTThuc;
        private System.Windows.Forms.RadioButton rdtCacPhongBan;
        private DataGrid dgrMucLuongBQ;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmMucLuongBQ()
		{
			InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmMucLuongBQ));
            this.rdtToanCongTy = new System.Windows.Forms.RadioButton();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.butKeTThuc = new System.Windows.Forms.Button();
            this.butChon = new System.Windows.Forms.Button();
            this.panel1 = new System.Windows.Forms.Panel();
            this.rdtCacPhongBan = new System.Windows.Forms.RadioButton();
            this.dgrMucLuongBQ = new System.Windows.Forms.DataGrid();
            this.groupBox1.SuspendLayout();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dgrMucLuongBQ)).BeginInit();
            this.SuspendLayout();
            // 
            // rdtToanCongTy
            // 
            this.rdtToanCongTy.Checked = true;
            this.rdtToanCongTy.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.rdtToanCongTy.ForeColor = System.Drawing.Color.Blue;
            this.rdtToanCongTy.Location = new System.Drawing.Point(12, 5);
            this.rdtToanCongTy.Name = "rdtToanCongTy";
            this.rdtToanCongTy.Size = new System.Drawing.Size(104, 24);
            this.rdtToanCongTy.TabIndex = 0;
            this.rdtToanCongTy.TabStop = true;
            this.rdtToanCongTy.Text = "Toàn đơn vị";
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.butKeTThuc);
            this.groupBox1.Controls.Add(this.butChon);
            this.groupBox1.Location = new System.Drawing.Point(137, 215);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(195, 42);
            this.groupBox1.TabIndex = 2;
            this.groupBox1.TabStop = false;
            // 
            // butKeTThuc
            // 
            this.butKeTThuc.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butKeTThuc.ForeColor = System.Drawing.Color.DarkBlue;
            this.butKeTThuc.Image = ((System.Drawing.Image)(resources.GetObject("butKeTThuc.Image")));
            this.butKeTThuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butKeTThuc.Location = new System.Drawing.Point(98, 11);
            this.butKeTThuc.Name = "butKeTThuc";
            this.butKeTThuc.Size = new System.Drawing.Size(75, 28);
            this.butKeTThuc.TabIndex = 4;
            this.butKeTThuc.Text = "Kết thúc";
            this.butKeTThuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butKeTThuc.Click += new System.EventHandler(this.butKeTThuc_Click);
            // 
            // butChon
            // 
            this.butChon.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butChon.ForeColor = System.Drawing.Color.DarkBlue;
            this.butChon.Image = ((System.Drawing.Image)(resources.GetObject("butChon.Image")));
            this.butChon.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butChon.Location = new System.Drawing.Point(22, 11);
            this.butChon.Name = "butChon";
            this.butChon.Size = new System.Drawing.Size(70, 28);
            this.butChon.TabIndex = 1;
            this.butChon.Text = "  Chọn";
            this.butChon.Click += new System.EventHandler(this.butChon_Click);
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.rdtCacPhongBan);
            this.panel1.Controls.Add(this.rdtToanCongTy);
            this.panel1.Location = new System.Drawing.Point(96, 176);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(264, 33);
            this.panel1.TabIndex = 3;
            // 
            // rdtCacPhongBan
            // 
            this.rdtCacPhongBan.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.rdtCacPhongBan.ForeColor = System.Drawing.Color.Blue;
            this.rdtCacPhongBan.Location = new System.Drawing.Point(145, 5);
            this.rdtCacPhongBan.Name = "rdtCacPhongBan";
            this.rdtCacPhongBan.Size = new System.Drawing.Size(116, 24);
            this.rdtCacPhongBan.TabIndex = 4;
            this.rdtCacPhongBan.Text = "Các phòng ban";
            // 
            // dgrMucLuongBQ
            // 
            this.dgrMucLuongBQ.AlternatingBackColor = System.Drawing.Color.Lavender;
            this.dgrMucLuongBQ.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dgrMucLuongBQ.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dgrMucLuongBQ.CaptionBackColor = System.Drawing.Color.LightSteelBlue;
            this.dgrMucLuongBQ.CaptionForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrMucLuongBQ.CaptionText = "Mức lương bình quân";
            this.dgrMucLuongBQ.CaptionVisible = false;
            this.dgrMucLuongBQ.DataMember = "";
            this.dgrMucLuongBQ.FlatMode = true;
            this.dgrMucLuongBQ.Font = new System.Drawing.Font("Tahoma", 8F);
            this.dgrMucLuongBQ.ForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrMucLuongBQ.GridLineColor = System.Drawing.Color.Gainsboro;
            this.dgrMucLuongBQ.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
            this.dgrMucLuongBQ.HeaderBackColor = System.Drawing.Color.MidnightBlue;
            this.dgrMucLuongBQ.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
            this.dgrMucLuongBQ.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
            this.dgrMucLuongBQ.LinkColor = System.Drawing.Color.Teal;
            this.dgrMucLuongBQ.Location = new System.Drawing.Point(12, 8);
            this.dgrMucLuongBQ.Name = "dgrMucLuongBQ";
            this.dgrMucLuongBQ.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
            this.dgrMucLuongBQ.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrMucLuongBQ.RowHeaderWidth = 10;
            this.dgrMucLuongBQ.SelectionBackColor = System.Drawing.Color.CadetBlue;
            this.dgrMucLuongBQ.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
            this.dgrMucLuongBQ.Size = new System.Drawing.Size(450, 156);
            this.dgrMucLuongBQ.TabIndex = 4;
            // 
            // frmMucLuongBQ
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(472, 269);
            this.Controls.Add(this.dgrMucLuongBQ);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.groupBox1);
            this.ForeColor = System.Drawing.Color.Blue;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "frmMucLuongBQ";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Mức lương bình quân";
            this.Load += new System.EventHandler(this.frmMucLuongBQ_Load);
            this.groupBox1.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.dgrMucLuongBQ)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion

		private void butKeTThuc_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
		private void LuongToanCty()
		{
		   try
			{   
				DataSet ads=f_MucluongBQ1();
				dgrMucLuongBQ.DataSource=null;
				dgrMucLuongBQ.TableStyles.Clear();
				DataGridTableStyle ts = new DataGridTableStyle();
				ts.MappingName = ads.Tables[0].TableName;
					
				ts.AlternatingBackColor = Color.Beige;
				ts.BackColor = Color.GhostWhite;
				ts.ForeColor = Color.MidnightBlue;
				ts.GridLineColor = Color.RoyalBlue;
				ts.HeaderBackColor = Color.MidnightBlue;
				ts.HeaderForeColor = Color.Lavender;
				ts.SelectionBackColor = Color.Teal;
				ts.SelectionForeColor = Color.PaleGreen;
				
				DataGridTextBoxColumn TextCol1=new DataGridTextBoxColumn();
				TextCol1.MappingName = "tongnv";
				TextCol1.HeaderText = lan.Change_language_MessageText("Tổng Số NV");
				TextCol1.Width = 70;
				TextCol1.Alignment=HorizontalAlignment.Center;
				ts.GridColumnStyles.Add(TextCol1);
				dgrMucLuongBQ.TableStyles.Add(ts);
			
				DataGridTextBoxColumn TextCol2=new DataGridTextBoxColumn();
				TextCol2.MappingName = "binhquanl";
				TextCol2.HeaderText = lan.Change_language_MessageText("Bình Quân");
				TextCol2.Width = 90;
				TextCol2.Alignment=HorizontalAlignment.Right;
                TextCol2.Format = "#,##0.00"; 
				ts.GridColumnStyles.Add(TextCol2);
				dgrMucLuongBQ.TableStyles.Add(ts);
			
				DataGridTextBoxColumn TextCol3=new DataGridTextBoxColumn();
				TextCol3.MappingName = "mlcn";
				TextCol3.HeaderText = lan.Change_language_MessageText("Mức Lương Cao Nhất");
				TextCol3.Width = 120;
                TextCol3.Format = "#,##0.00"; 
				TextCol3.Alignment=HorizontalAlignment.Right;
				ts.GridColumnStyles.Add(TextCol3);
				dgrMucLuongBQ.TableStyles.Add(ts);
			
				DataGridTextBoxColumn TextCol4=new DataGridTextBoxColumn();
				TextCol4.MappingName = "mltn";
				TextCol4.HeaderText = lan.Change_language_MessageText("Mức Lương Thấp Nhất");
				TextCol4.Width = 160;
                TextCol4.Format = "#,##0.00"; 
				TextCol4.Alignment=HorizontalAlignment.Right;
				ts.GridColumnStyles.Add(TextCol4);
				dgrMucLuongBQ.TableStyles.Add(ts);
			
				ts.DataGrid.DataSource=ads.Tables[0];

                CurrencyManager cm = (CurrencyManager)BindingContext[dgrMucLuongBQ.DataSource, dgrMucLuongBQ.DataMember];
                DataView dv = (DataView)cm.List;
                dv.AllowDelete = false;
                dv.AllowEdit = false;
                dv.AllowNew = false;
			}
			catch(Exception ex)
			{
			   MessageBox.Show(ex.ToString());
			}
		}
		private void LuongCacPB()
		{
			try
			{
			DataSet ads=f_MucluongBQ2();
			dgrMucLuongBQ.DataSource=null;
			dgrMucLuongBQ.TableStyles.Clear();
			DataGridTableStyle ts = new DataGridTableStyle();
			ts.MappingName = ads.Tables[0].TableName;
					
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
            ts.RowHeaderWidth = 10;
				
			DataGridTextBoxColumn TextCol1=new DataGridTextBoxColumn();
			TextCol1.MappingName = "Ten_BP_VNI";
			TextCol1.HeaderText = lan.Change_language_MessageText("Tên Bộ Phận");
			TextCol1.Width = 200;
			TextCol1.Alignment=HorizontalAlignment.Left;
			ts.GridColumnStyles.Add(TextCol1);
			dgrMucLuongBQ.TableStyles.Add(ts);
			
			DataGridTextBoxColumn TextCol2=new DataGridTextBoxColumn();
			TextCol2.MappingName = "tong";
			TextCol2.HeaderText = lan.Change_language_MessageText("Tổng Số");
			TextCol2.Width = 60;
			TextCol2.Alignment=HorizontalAlignment.Left;
			ts.GridColumnStyles.Add(TextCol2);
			dgrMucLuongBQ.TableStyles.Add(ts);
			
			DataGridTextBoxColumn TextCol3=new DataGridTextBoxColumn();
			TextCol3.MappingName = "luongbq";
			TextCol3.HeaderText = lan.Change_language_MessageText("Lương Bình Quân");
			TextCol3.Width = 150;
			TextCol3.Alignment=HorizontalAlignment.Left;
			ts.GridColumnStyles.Add(TextCol3);
			dgrMucLuongBQ.TableStyles.Add(ts);
			
			ts.DataGrid.DataSource=ads.Tables[0];
            CurrencyManager cm = (CurrencyManager)BindingContext[dgrMucLuongBQ.DataSource, dgrMucLuongBQ.DataMember];
            DataView dv = (DataView)cm.List;
            dv.AllowDelete = false;
            dv.AllowEdit = false;
            dv.AllowNew = false;
		}
		catch{}
		
	}

		private void butChon_Click(object sender, System.EventArgs e)
		{
			if (rdtToanCongTy.Checked==true)
			{
				try
				{
					LuongToanCty();
				}
				catch{}
			}
			if(rdtCacPhongBan.Checked==true)
			{
				try
				{
					LuongCacPB();
				}
				catch{}
			}
		 }

        private void frmMucLuongBQ_Load(object sender, EventArgs e)
        {

        }
        private DataSet f_MucluongBQ2()
        {
            string sql = "";
            sql =" SELECT COUNT(hd.manv) AS TONG," ;
            sql += " bp.tenbp as TEN_BP_VNI," ;
            sql += " round(sum(hd.LUONGCT)/count(hd.manv),2) AS LUONGBQ ";
            sql += " from " + mDB.user + ".hopdong hd left join " + mDB.user + ".bophan bp on hd.madv=bp.madv";
            sql += " inner join "+mDB.user+".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan  " ;
            sql += " and (hd.ngaynghi is null or to_date(to_char(ngaynghi,'dd/mm/yyyy'),'dd/mm/yyyy') > to_date('" + DateTime.Now.Date.ToString("dd/MM/yyyy") + "','dd/mm/yyyy'))";
            sql += " group by hd.madv,bp.tenbp,bp.sothutu";
            sql += " order by bp.sothutu";
            return mDB.get_data(sql);
        }
        private DataSet f_MucluongBQ1()
        {
            string sql = "";
            sql= "SELECT Count(hd.MANV) AS TongNV,";
            sql += " round(Avg(hd.LUONGCT),2) AS BinhQuanL,";
            sql += " Max(hd.LUONGCT) AS MLCN, Min(hd.LUONGCT) AS MLTN";
            sql += " from "+mDB.user+".llnv l inner join " + mDB.user + ".hopdong hd on l.manv=hd.manv";
            sql += " left JOIN " + mDB.user + ".bophan bp ON hd.MADV = bp.madv";
            sql += " inner join "+mDB.user+".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan ";
            sql += " and (hd.ngaynghi is null or to_date(to_char(ngaynghi,'dd/mm/yyyy'),'dd/mm/yyyy') > to_date('" + DateTime.Now.Date.ToString("dd/MM/yyyy") + "','dd/mm/yyyy'))";
            return mDB.get_data(sql);
        }
	}
}
